var jwt = require('jsonwebtoken');
var fs = require('fs');
var priv_key = fs.readFileSync('./priv.txt','utf8').toString();
var pub_key = fs.readFileSync('./pub.txt','utf8').toString();

// 签名
var token = jwt.sign({ name: 'foo-bar',permission:'order',action:'cancel',orderNum:'8892-323-4343' }
	, priv_key
	,{
		algorithm: 'RS256',
		issuer:'kevin',
		audience:'jimmy',
		subject:'alice',
		jwtid:'jwtid',
		expiresIn:'1h'
	});

console.log(token);


// 验证
try{
	jwt.verify(token, pub_key,{
		algorithms: ['RS256'],
		issuer:'kevin',
		audience:'jimmy',
		subject:'alice',
		jwtid:'jwtid'
	},function(err,payload){
		if(err){
			console.log('身份认证失败：'+err.message);
		}
		else{
			console.log(payload);
			// console.log(decoded.payload);
		}
	});
	//console.log("验证："+JSON.stringify(decoded));
}
catch(err){
	console.log(err.message);
}


// 解析header和payload
var decoded = jwt.decode(token,{complete: true});
console.log('--Header:');
console.log(decoded.header);
console.log('--PayLoad:');
console.log(decoded.payload);